﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace EWMSWeb.CumMain
{
    public partial class UploadImg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtDate.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
                if(Request["loadid"]!=null && Request["loadid"].Trim()!="")
                    bindRept(Request["loadid"].Trim());
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataLib.BLL.LoadNum bll = new DataLib.BLL.LoadNum();
            DataLib.Model.Load_Img m = new DataLib.Model.Load_Img();
            DataLib.BLL.Load_Img b = new DataLib.BLL.Load_Img();
            DataLib.BLL.load_att ba = new DataLib.BLL.load_att();
            DataLib.Model.load_att am = new DataLib.Model.load_att();
            DataTable tabload = bll.GetList("loadnum='" + txtLoad.Text.Trim() + "'").Tables[0];
           if (!FileUpload1.HasFile)
            {
                DataLib.Common.MessageBox.Show(Page, "请选择上传的图片");
                return;
            }

            m.loadid = int.Parse(tabload.Rows[0]["id"].ToString());
            m.loadnum = txtLoad.Text.Trim();
            m.StepFlag = DropDownList1.SelectedItem.Text;
            m.cjoid = 0;
            m.createid = 0;
            m.createtime = DateTime.Now;
            m.flag1 = tabload.Rows[0]["cumname"].ToString();
            m.flag2 = txtDate.Text.ToString();
            m.Step = int.Parse(DropDownList1.SelectedValue);
            
            string filename = m.loadnum + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".JPG"; ;
            string strPhotoPath = Server.MapPath("~/upload/Load/" + filename);
            FileUpload1.PostedFile.SaveAs(strPhotoPath);
            m.FileName = filename;
          
            if (b.Add(m))
            {
                am = ba.GetLModel((int)m.loadid);
                if (am != null)
                {
                    switch (m.Step)
                    {
                        case 1:
                            if (am.loadcolldate == null)
                                am.loadcolldate = DateTime.Parse(txtDate.Text);
                            break;
                        case 2:
                            if (am.loadindate == null)
                                am.loadindate = DateTime.Parse(txtDate.Text);

                            break;
                        case 3:
                            if (am.loadoutdate == null)
                                am.loadoutdate = DateTime.Parse(txtDate.Text);

                            break;
                        case 4:
                            if (am.loadcjdate == null)
                                am.loadcjdate = DateTime.Parse(txtDate.Text);
                            break;
                        default:
                            break;
                    }

                    if (am.loadState < m.Step)
                    {
                        am.loadState = m.Step;
                    }
                    ba.Update(am);
                }
                else
                {
                    am = new DataLib.Model.load_att();
                    am.loadid = m.loadid;
                    am.loadState = m.Step;
                    am.flag1 = m.loadnum;
                    switch (m.Step)
                    {
                        case 1:

                            am.loadcolldate = DateTime.Parse(txtDate.Text);
                            break;
                        case 2:
                            am.loadindate = DateTime.Parse(txtDate.Text);

                            break;
                        case 3:
                            am.loadoutdate = DateTime.Parse(txtDate.Text);

                            break;
                        case 4:
                            am.loadcjdate = DateTime.Parse(txtDate.Text);
                            break;
                        default:
                            break;
                    }
                    ba.Add(am);
                }
             
                bindRept(m.loadid.ToString());
            }
        }
        protected void bindRept(string loadid)
        {
            DataTable dt = new DataTable();
            DataLib.BLL.Load_Img bll = new DataLib.BLL.Load_Img();
            DataLib.BLL.LoadNum b = new DataLib.BLL.LoadNum();
            DataLib.Model.LoadNum m = new DataLib.Model.LoadNum();
            m = b.GetModel(int.Parse(loadid));
            txtLoad.Text = m.loadnum;
            dt = bll.GetList("loadid="+loadid).Tables[0];
            rept.DataSource = dt;
            rept.DataBind();
        }
    }
}